#include "VJsonItemWidget.h"
#include "ui_VJsonItemWidget.h"
#include "VGlobal.h"

#include <QColorDialog>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QListView>
#include <QKeyEvent>

VJsonItemWidget::VJsonItemWidget(QWidget *parent, QString type, QString title, QString value) :
    QWidget(parent),
    ui(new Ui::VJsonItemWidget)
{
    ui->setupUi(this);
    m_type = type;

    ui->widget->setContentsMargins(0, 0 , 0, 0);
    ui->widget->layout()->setContentsMargins(0, 0, 0, 0);
    ui->comboBox->setView(new QListView());

    ui->checkBox->hide();
    ui->fontComboBox->hide();
    ui->lineEdit->hide();
    ui->label->hide();
    ui->spinBox->hide();
    ui->comboBox->hide();
    ui->widget->hide();

    if(title.isEmpty() == false)
    {
        ui->label->setText(title);
        ui->label->show();
    }

    if(type == "b")
    {
        ui->label->hide();
        ui->checkBox->setText(title);
        ui->checkBox->setChecked(value == "true");
        ui->checkBox->show();
    }
    else if(type == "font")
    {
        ui->fontComboBox->setCurrentText(value);
        ui->fontComboBox->show();
    }
    else if(type == "s")
    {
        ui->lineEdit->setText(value);
        ui->lineEdit->show();
    }
    else if(type == "i")
    {
        ui->spinBox->setValue(value.toInt());
        ui->spinBox->show();
    }
    else if(type == "c")
    {
        //ui->label_2->setStyleSheet(QString("background:#%1").arg(value.toInt(nullptr, 16), 6, 16, QLatin1Char('0')));
        ui->lineEdit_color->setText(value);
        ui->widget->show();
    }
    else if(type == "skin")
    {
        QStringList list = get_skins_names();
        ui->comboBox->addItems(list);
        ui->comboBox->setCurrentText(value);
        ui->comboBox->show();
    }
    else if(type == "t")
    {
        ui->lineEdit->setText(value);
        ui->lineEdit->show();
    }
}

VJsonItemWidget::~VJsonItemWidget()
{

    delete ui;
}

QString VJsonItemWidget::get_value()
{
//    qDebug() << "get value";
    if(m_type == "b")
    {
        return ui->checkBox->isChecked()?"true":"false";
    }
    else if(m_type == "font")
    {
        return ui->fontComboBox->currentFont().family();
    }
    else if(m_type == "s")
    {
        return ui->lineEdit->text();
    }
    else if(m_type == "i")
    {
        return QString::number(ui->spinBox->value());
    }
    else if(m_type == "c")
    {
        return ui->lineEdit_color->text();
    }
    else if(m_type == "skin")
    {
        return ui->comboBox->currentText();
    }
    else if(m_type == "t")
    {
        return ui->lineEdit->text();
    }

    return "";
}

//void VJsonItemWidget::on_lineEdit_2_textChanged(const QString &arg1)
//{

//}

//void VJsonItemWidget::on_pushButton_clicked()
//{

//}

QString VJsonItemWidget::makeColorString(const QColor &color, const QString type)
{
    if(type == "RGBA") {
        return QString("rgba(%1, %2, %3, %4)")
                .arg(color.red())
                .arg(color.green())
                .arg(color.blue())
                .arg(color.alpha());
    }
    if(type == "RGB") {
        return QString("rgba(%1, %2, %3)")
                .arg(color.red())
                .arg(color.green())
                .arg(color.blue());
    }
    if(type == "HEX") {
        return QString().sprintf("%02X%02X%02X",
                                 color.red(),
                                 color.green(),
                                 color.blue());
    }

    return color.name();
}

QStringList VJsonItemWidget::get_skins_names()
{
    QStringList list;
    QDir dir(VGlobal::skins_dir);
    foreach(QFileInfo mfi ,dir.entryInfoList())
    {
        if(mfi.isFile() == false)
        {
            if(mfi.fileName()=="." || mfi.fileName() == "..")continue;
            list.append(mfi.fileName());
        }
    }
    return list;
}

bool VJsonItemWidget::eventFilter(QObject *watched, QEvent *event)
{
//    static bool release = true;
//    if(watched == ui->lineEdit)
//    {
//        if(event->type() == QEvent::KeyPress)
//        {
//            if(release)
//            {
//                ui->lineEdit->clear();
//                release = false;
//            }

//            QString pre = "";
//            if(ui->lineEdit->text().isEmpty())
//            {
//                pre = "";
//            }
//            else
//            {
//                pre = "+";
//            }

//            QKeyEvent * keyEvent = (QKeyEvent*)event;
//            if(keyEvent->key() == Qt::Key_Alt)
//            {
//                ui->lineEdit->setText(ui->lineEdit->text() + pre + "Alt");
//                return false;
//            }
//            else if(keyEvent->key() == Qt::Key_Control)
//            {
//                ui->lineEdit->setText(ui->lineEdit->text() + pre + "Ctrl");
//                return false;
//            }
//            else if(keyEvent->key() == Qt::Key_Shift)
//            {
//                ui->lineEdit->setText(ui->lineEdit->text() + pre + "Shift");
//                return false;
//            }
//        }
//        else if(event->type() == QEvent::KeyRelease)
//        {
//            release = true;
//        }
//    }

    return QWidget::eventFilter(watched, event);
}


void VJsonItemWidget::on_lineEdit_color_textChanged(const QString &arg1)
{
    ui->frame_color->setStyleSheet(QString("background:#%1")
                                   .arg(ui->lineEdit_color->text().toInt(nullptr, 16),
                                        6, 16, QLatin1Char('0')));
}

void VJsonItemWidget::on_btn_selectColor_clicked()
{
    QColor color = QColorDialog::getColor(QColor(ui->lineEdit_color->text().toInt(nullptr, 16)), this, "选择颜色");
    if(color.isValid())
    {
        qDebug() << color.value();
        ui->lineEdit_color->setText(makeColorString(color, "HEX"));
    }
}
